#pragma once
#ifndef __SHADERCONSTANTBUFFER_H__
#define __SHADERCONSTANTBUFFER_H__

#include "Utility.h"

#include "../util/PreDeclare.h"
#include "../util/D3DCommon.h"

#include <unordered_map>

namespace WindGE
{
	class WIND_CORE_API ConstantBuffer
	{
	public:
		ConstantBuffer(RenderDevicePtr renderDevice);
		~ConstantBuffer();

		bool create(ID3D11ShaderReflection* shaderReflection, ID3D11ShaderReflectionConstantBuffer* reflectionCBuffer);

		bool update(void* databuff, unsigned int datasize);
		
		void bind_vertex();
		void bind_geometry();
		void bind_hull();
		void bind_domain();
		void bind_pixel();

		inline const char* name() const { return buffer_desc_.Name; }

	private:
		bool map(void** dataptr, unsigned int subres, D3D11_MAP maptype);
		void unmap(unsigned int subres);

	private:
		RenderDevicePtr										render_device_;
		ID3D11ShaderReflection*								shader_reflection_;
		ID3D11ShaderReflectionConstantBuffer*				reflection_cbuffer_;
		
		ID3D11Buffer*										d3d_buffer_;
		void*												mem_buff_;
		D3D11_MAPPED_SUBRESOURCE							mapped_resource_;
		
		D3D11_SHADER_BUFFER_DESC							buffer_desc_;

		ShaderBindPtr										shader_bind_;

		std::unordered_map<std::string, ShaderVariablePtr>	variable_list_;
	};

}//end namespace WindGE

#endif	//__SHADERCONSTANTBUFFER_H__
